package com.example.emos.wx.message.controller;

import com.example.emos.wx.core.common.util.R;
import com.example.emos.wx.message.model.form.DeleteMessageRefByIdForm;
import com.example.emos.wx.message.model.form.SearchMessageByIdForm;
import com.example.emos.wx.message.model.form.SearchMessageByPageForm;
import com.example.emos.wx.message.model.form.UpdateUnreadMessageForm;
import com.example.emos.wx.message.service.MessageService;
import com.example.emos.wx.message.task.MessageTask;
import com.example.emos.wx.user.shiro.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * @Author YinXi
 * @Versin 1.0.0
 * @Date 2023/9/12
 */
@RestController
@RequestMapping("/message")
@Api("消息模块网络接口")
public class MessageController {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private MessageService messageService;
    @Autowired
    private MessageTask messageTask;

    @PostMapping("/searchMessageByPage")
    @ApiOperation("获取分页消息列表")
    public R searchMessageByPage(@Valid @RequestBody SearchMessageByPageForm form, @RequestHeader("token") String token){
        int userId = jwtUtil.getUserIdFromJwt(token);
        int page = form.getPage();
        int length = form.getLength();
        long start =  (page-1)*length;
        List<HashMap> list = messageService.searchMessageByPage(userId, start, length);
        return R.ok().put("result",list);
    }

    @PostMapping("/searchMessageById")
    @ApiOperation("根据Id查询消息")
    public R searchMessageById(@Valid @RequestBody SearchMessageByIdForm form){
        return R.ok().put("result",messageService.searchMessageById(form.getId()));
    }

    @PostMapping("/updateUnreadMessage")
    @ApiOperation("未读消息更新为已读消息")
    public R updateUnreadMessage(@Valid @RequestBody UpdateUnreadMessageForm form){
        long rows = messageService.updateUnreadMessage(form.getId());
        return R.ok().put("result",rows==1?true:false);
    }

    @PostMapping("/deleteMessageRefById")
    @ApiOperation("删除消息")
    public R deleteMessageRefById(@Valid @RequestBody DeleteMessageRefByIdForm form){
        long rows = messageService.deleteMessageRefById(form.getId());
        return R.ok().put("result",rows==1?true:false);
    }

    @GetMapping("/refreshMessage")
    @ApiOperation("刷新用户的消息")
    public R refreshMessage(@RequestHeader("token")String token ){
        int userId = jwtUtil.getUserIdFromJwt(token);
        //异步接收消息
        messageTask.receiveAsync(userId+"");
        //查询接收了多少条消息
        long lastRows = messageService.searchLastCount(userId);
        //查询未读数据
        long unreadRows = messageService.searchUnreadCount(userId);
        return R.ok().put("lastRows",lastRows).put("unreadRows",unreadRows);
    }
}
